package com.panshi.controller.article;

import com.alibaba.fastjson.JSON;
import com.panshi.api.ArticleApi;
import com.panshi.api.UserApi;
import com.panshi.po.ArticlePo;
import com.panshi.po.UserPO;
import com.panshi.vo.*;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class Into {

    @Resource
    private ArticleApi articleApi;

    @Resource
    private UserApi userApi;

    @GetMapping("/classify")
    public String infoClassify(){

        return "article/category";
    }

    //详情
    @GetMapping("/detail/{number}")
    public ModelAndView detail(@PathVariable Integer number){
        ModelAndView mv = new ModelAndView("article/detail");

        ArticleNumberQueryVo queryVo = new ArticleNumberQueryVo();
        queryVo.setNumber(number);
        ResponseVO<ArticlePo> vo = articleApi.numberQuery(queryVo);
        ArticlePo po = JSON.parseObject(JSON.toJSONString(vo.getData()),ArticlePo.class);

        //查询所有数据（推荐），下架的除外
        PageBeanVo page = new PageBeanVo();
        page.setCurrentPage(1);
        page.setPageSize(8);
        ResponseVO<List<ArticlePo>> res = articleApi.soldQuery(page);
        List<ArticlePo> data = res.getData();

        log.info("po========================={}",po);

        mv.addObject("data",data);
        mv.addObject("article",po);

        return mv;
    }

    @GetMapping("/infoclassify/{sortname}")
    public ModelAndView infoClassify(@RequestParam String sortname){
        ModelAndView mv = new ModelAndView("article/category");
        ArticleSortNameQueryVo queryVo = new ArticleSortNameQueryVo();
        queryVo.setSortname(sortname);
        queryVo.setCurrentPage(1);
        queryVo.setPageSize(10);
        ResponseVO<List<ArticlePo>> vo = articleApi.sortNameQuery(queryVo);
        List<ArticlePo> list = vo.getData();

        mv.addObject("sortnamelist",list);
        return mv;
    }

    @PostMapping("/type")
    @ResponseBody
    public ModelAndView infor(@RequestParam Integer type){
        ModelAndView mv = new ModelAndView("article/information_data");
        //获取所有数据（推荐），下架的除外
        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setCurrentPage(1);
        pageBeanVo.setPageSize(10);
        ResponseVO<List<ArticlePo>> vo = null;
        if(type == 10){//推荐
            vo = articleApi.soldQuery(pageBeanVo);
        }else if(type==15){//热门
            vo = articleApi.pvLimitQuery(pageBeanVo);
        }else if(type==20){//最新
            vo = articleApi.timeLimtQuery(pageBeanVo);
        }
        List<ArticlePo> article = JSON.parseArray(JSON.toJSONString(vo.getData()),ArticlePo.class);

        List<InformationVO> list = new ArrayList<>();

        for (ArticlePo po:article) {
            InformationVO informationVO = new InformationVO();
            BeanUtils.copyProperties(po,informationVO);

            ResponseVO<UserPO> userResponse = userApi.findUserById(po.getUid());
            UserPO userPO = JSON.parseObject(JSON.toJSONString(userResponse.getData()),UserPO.class);

            informationVO.setUser(userPO);

            list.add(informationVO);
        }
        log.info("--------------------------article:{}",list);

        mv.addObject("list",list);
        return mv;
    }

    @PostMapping("/load")
    @ResponseBody
    public ModelAndView infor(@RequestParam Integer type,@RequestParam Integer page){
        ModelAndView mv = new ModelAndView("article/information_data");
        //获取所有数据（推荐），下架的除外
        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setCurrentPage(page);
        pageBeanVo.setPageSize(10);
        ResponseVO<List<ArticlePo>> vo = null;
        if(type == 10){//推荐
            vo = articleApi.soldQuery(pageBeanVo);
        }else if(type==15){//热门
            vo = articleApi.pvLimitQuery(pageBeanVo);
        }else if(type==20){//最新
            vo = articleApi.timeLimtQuery(pageBeanVo);
        }
        List<ArticlePo> article = JSON.parseArray(JSON.toJSONString(vo.getData()),ArticlePo.class);

        List<InformationVO> list = new ArrayList<>();

        for (ArticlePo po:article) {
            InformationVO informationVO = new InformationVO();
            BeanUtils.copyProperties(po,informationVO);

            ResponseVO<UserPO> userResponse = userApi.findUserById(po.getUid());
            UserPO userPO = JSON.parseObject(JSON.toJSONString(userResponse.getData()),UserPO.class);

            informationVO.setUser(userPO);

            list.add(informationVO);
        }
        log.info("--------------------------article:{}",article);

        mv.addObject("list",list);
        return mv;
    }

    //资讯 已解决
    @GetMapping("/information")
    public ModelAndView information(){
        ModelAndView mv = new ModelAndView("article/information");
        //获取所有数据（推荐），下架的除外
        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setCurrentPage(1);
        pageBeanVo.setPageSize(10);
        ResponseVO<List<ArticlePo>> vo = articleApi.soldQuery(pageBeanVo);
        List<ArticlePo> article = JSON.parseArray(JSON.toJSONString(vo.getData()),ArticlePo.class);

        List<InformationVO> list = new ArrayList<>();

        for (ArticlePo po:article) {
            InformationVO informationVO = new InformationVO();
            BeanUtils.copyProperties(po,informationVO);

            ResponseVO<UserPO> userResponse = userApi.findUserById(po.getUid());
            UserPO userPO = JSON.parseObject(JSON.toJSONString(userResponse.getData()),UserPO.class);

            informationVO.setUser(userPO);

            list.add(informationVO);
        }

        log.info("-----------------------list{}",list);

        //根据阅读量排序查询(大家都在看)，获取10条数据
        ResponseVO<List<ArticlePo>> ranking = articleApi.pvLimitQuery(pageBeanVo);
        List<ArticlePo> rankingList = ranking.getData();

        //查询所有数据（推荐），下架的除外，获取4条数据，包含图片
        PageBeanVo page = new PageBeanVo();
        page.setCurrentPage(1);
        page.setPageSize(4);
        ResponseVO<List<ArticlePo>> res = articleApi.soldQuery(page);
        List<ArticlePo> articleData = res.getData();

        log.info("***************************article:{}",article);
        mv.addObject("ranking",rankingList);
        mv.addObject("list",list);
        mv.addObject("rec",articleData);
        return mv;
    }

    @GetMapping("/category/{category}")
    public ModelAndView category(@PathVariable String category) throws UnsupportedEncodingException {
        ModelAndView mav = new ModelAndView("article/information");
        ArticleSortNameQueryVo queryVo = new ArticleSortNameQueryVo();
        category = URLDecoder.decode(category,"utf-8");
        queryVo.setSortname(category);
        queryVo.setCurrentPage(1);
        queryVo.setPageSize(10);
        ResponseVO<List<ArticlePo>> responseVO = articleApi.sortNameQuery(queryVo);

        List<ArticlePo> articleList = JSON.parseArray(JSON.toJSONString(responseVO.getData()),ArticlePo.class);

        List<InformationVO> list = new ArrayList<>();

        for (ArticlePo po:articleList) {
            InformationVO informationVO = new InformationVO();
            BeanUtils.copyProperties(po,informationVO);

            ResponseVO<UserPO> userResponse = userApi.findUserById(po.getUid());
            UserPO userPO = JSON.parseObject(JSON.toJSONString(userResponse.getData()),UserPO.class);

            informationVO.setUser(userPO);

            list.add(informationVO);
        }

        log.info("********************************{}",list);

        mav.addObject("list",list);
        return mav;
    }



}
